package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.MarketAddress;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.bo.wx.WxAddressSaveBo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.wx.WxAddressService;
import com.cskaoyan.utils.ValidationUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanInfoFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 收货地址
 * /wx/address
 *
 * @Author: zhang
 * @Date: 2022/11/22 17:07
 * @Since: jdk1.8
 */
@RestController
@RequestMapping("wx/address")
public class WxAddressController {
    @Autowired
    WxAddressService wxAddressService;

    /**
     * 查询所有的收货地址
     * url:wx/address/list
     *
     * @return BaseRespVo<Object>
     */
    @GetMapping("list")
    public BaseRespVo<Object> list() {
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.error(501, "请登录");
        }
        Integer userId = user.getId();
        CommonData<MarketAddress> data = wxAddressService.list(userId);
        return BaseRespVo.ok(data);
    }

    /**
     * 收货地址详情
     *
     * @param id 地址id
     * @return ok
     * @throws ValidationException 参数异常信息
     */
    @GetMapping("detail")
    public BaseRespVo<Object> detail(Integer id) throws ValidationException {
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.error(501, "请登录");
        }
        MarketAddress data = wxAddressService.detail(id);
        return BaseRespVo.ok(data);
    }

    /**
     * 新增一个收货地址
     *
     * @param wxAddressSaveBo 新增的地址信息
     * @param bindingResult   异常信息
     * @return 新增的主键
     */
    @PostMapping("save")
    public BaseRespVo<Integer> save(@RequestBody @Validated WxAddressSaveBo wxAddressSaveBo, BindingResult bindingResult) {
        ValidationUtil.validate(bindingResult);
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.error(501, "请登录");
        }
        Integer userId = user.getId();
        int lastInsertPrimaryKey = wxAddressService.save(userId, wxAddressSaveBo);
        return BaseRespVo.ok(lastInsertPrimaryKey);
    }

    /**
     * 删除收货地址
     *
     * @param map id
     * @return ok
     */
    @RequestMapping("delete")
    public BaseRespVo<Object> delete(@RequestBody Map<String, String> map) {
        String id = map.get("id");
        int affectedRows = wxAddressService.delete(id);
        return BaseRespVo.ok();
    }
}
